<?php
/**
 * Nweb Framework
 *
 * This script is protected by copyright. It's use, copying, modification
 * and distribution without written consent of the author is prohibited.
 *
 * @category    source
 * @package     pl.nweb.framework.autoloader
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2011 Krzysztof Kardasz
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt  GNU Lesser General Public
 * @version     3.0 $Id$
 * @link        http://code.google.com/p/nweb-framework/
 * @link        http://framework.nweb.pl
 */

namespace pl\nweb\framework\autoloader;

/**
 * Autoloader biblioteki Zend Framework X
 *
 * @category    source
 * @package     pl.nweb.framework.autoloader
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2011 Krzysztof Kardasz
 * @version     3.0 $Id$
 */
class ZendX implements iAutoloader
{
    /**
     * Źródła
     *
     * @var string
     */
    private static $_src = null;

    /**
     * Wersja biblioteki Zend Framework X
     *
     * @var integer
     */
    private $_version = self::VERSION_1;

    /**
     * Wersje biblioteki Zend Framework X
     */
    const VERSION_1 = 1;
    const VERSION_2 = 2;

    /**
     * Konstruktor
     *
     * @param integer $version wersja Zend Framework
     */
    public function __construct ($version = null)
    {
        if(null !== $version) {
            $this->_version = $version;
        }
    }

    /**
     * Zwraca ścieżkę biblioteki zend framework x
     * Domyślna ścieżka: src/com/zend/framework
     *
     * @return string
     */
    public function getSrc ()
    {
        if(null === self::$_src) {
            self::$_src = substr(__DIR__, 0, -29) . 'com' . DIRECTORY_SEPARATOR . 'zend' . DIRECTORY_SEPARATOR . 'x';
        }
        return self::$_src;
    }

    /**
     * Definiuje ścieżkę biblioteki Zend Framework X
     *
     * @param  string $path ścieżka
     * @return void
     */
    public function setSrc ($path)
    {
        self::$_src = $path;
    }

    /**
     * Autoładowanie klasy
     *
     * @param  string $class nazwa klasy
     * @return boolean
     * @throws Exception
     */
    public function load ($class)
    {
        if(class_exists($class, false) || interface_exists($class, false)) {
            return true;
        }

        switch($this->_version) {
            case self::VERSION_1:
                return $this->_load1($class);
            break;

            case self::VERSION_2:
                return $this->_load1($class);
            break;

            default:
                throw new Exception ('Uknown Zend Framework X version "' . $this->_version . '"');
        }
    }

    /**
     * Ładowanie klas Zend Framework X w wersji 1.x
     *
     * @param string $class nazwa klasy
     * @return boolean
     */
    private function _load1 ($class)
    {
        if(substr($class, 0, 5) !== 'ZendX') {
            return false;
        }

        $file = str_replace('_', DIRECTORY_SEPARATOR, substr($class, 6)) . '.php';
        $path = $this->getSrc() . DIRECTORY_SEPARATOR . $file;

        if(is_file($path)) {
            include_once $path;
            return true;
        }

        return false;
    }

    /**
     * Ładowanie klas Zend Framework w wersji 2.x
     *
     * @param string $class nazwa klasy
     * @return boolean
     * @throws Exception
     */
    private function _load2 ($class)
    {
        throw new Exception ('Zend Framework 2 X is not supported');
    }
}